package com.namuan.jp_stock.dialog;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.namuan.jp_stock.activity.DetailActivity;
import com.namuan.jp_stock.activity.LoginActivity;
import com.namuan.jp_stock.activity.DetailActivity.LoadFullActivityRequestListener;
import com.namuan.jp_stock.activity.MainActivity.LoadActivityRequestListener;
import com.namuan.jp_stock.object.Stock;
import com.namuan.jp_stock.util.ActivityItemAdapter;
import com.namuan.jp_stock.util.AsyncJustplaceRunner;
import com.namuan.jp_stock.util.BaseRequestListener;
import com.namuan.jp_stock.util.CustomImageManager;
import com.namuan.jp_stock.util.Justplace;
import com.namuan.jp_stock.util.JustplaceError;
import com.namuan.jp_stock.util.ObjUtils;
import com.namuan.jp_stock.util.SessionStore;
import com.namuan.jp_stock.util.StockItemAdapter;
import com.namuan.jp_stock.util.Util;
import com.namuan.jp_stock.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SelectStockDialog extends Activity implements  OnClickListener, OnItemClickListener {

	private ArrayList<Stock> stockItems;
	private ListView stockListView;
	private StockItemAdapter stockAdapter;
	private SelectStockDialog mContext;
	private Justplace mJutplace;
	private AsyncJustplaceRunner mAsyncRunner;
	private ProgressDialog progressdialog;
	
	private EditText editStock;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.stock_selection);		
		mContext = this;
	
		editStock = (EditText) findViewById(R.id.editStock);
		Button imgBtnSearch = (Button) findViewById(R.id.imgBtnSearch); 
		stockListView = (ListView)findViewById(R.id.stockListView);
	
		stockItems = new ArrayList<Stock>();
		stockAdapter = new StockItemAdapter(this, R.layout.stock_list_row, stockItems);
		
		imgBtnSearch.setOnClickListener(this);
		
		stockListView.setAdapter(stockAdapter);
		stockAdapter.setNotifyOnChange(true);
		
		stockListView.setOnItemClickListener(this);
        
		mJutplace = new Justplace();
		SessionStore.restore(mJutplace, this);
		mAsyncRunner = new AsyncJustplaceRunner(mJutplace);
		
		init();
		
		
	}

	private void init() {

		progressdialog = new ProgressDialog(this);
    	progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    	progressdialog.setTitle("Loading");
    	progressdialog.setMessage("잠시만 기다려주세요");
    	progressdialog.show();
    	
    	Bundle params = new Bundle();
    	
    	/**/
    	mAsyncRunner.request("loadallstocks.do", params, "GET",
                 new LoadStockRequestListener(), null);
                 /***/
	}

	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Stock  Item = (Stock)stockAdapter.getItem(arg2);
		String stocknumber = Item.getStocknumber();
		String stockname = Item.getStockname();	
		Intent intent = new Intent();
		intent.putExtra("stocknumber", stocknumber);
		intent.putExtra("stockname", stockname);
		this.setResult(RESULT_OK, intent);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
			case R.id.imgBtnSearch://search
				String stockInfo = editStock.getText().toString();
				if(!stockInfo.equals("")) {

				progressdialog = new ProgressDialog(this);
		    	progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		    	progressdialog.setTitle("Loading");
		    	progressdialog.setMessage("잠시만 기다려주세요");
		    	progressdialog.show();
		    	
				Bundle params = new Bundle();
				params.putString("stockinfo", stockInfo);
				/**/mAsyncRunner.request("stockunionsearch.do", params, "GET",
						new LoadUnionSearchStocksRequestListener(), null);
				} else {
					
				}
				break;
		}
		
	}
	

	
	public class LoadStockRequestListener extends BaseRequestListener {

		public void onComplete(final String response, final Object state) {
			Log.d("Stock SNS-Main", "Response: " + response.toString());
			try {
				stockItems.clear();
				Log.d("Stock SNS-Main", "Response: " + response.toString());
				JSONObject json = Util.parseJson(response);
				final ArrayList<Stock> stocks = ObjUtils.jsonToStockList(json.getJSONArray("stockList"));
				
				for (Stock stock : stocks) {
					stockItems.add(stock);
				}
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						stockAdapter.notifyDataSetChanged();
						if(progressdialog.isShowing())
							progressdialog.dismiss();
					}
				});
			} catch (JSONException e) {
				Log.w("Stock SNS-Main", "JSON Error in response");
			} catch (JustplaceError e) {
				Log.w("Stock SNS-Main", "Stock SNS Error: " + e.getMessage());
			}
		}
    }
	
	public class LoadUnionSearchStocksRequestListener extends BaseRequestListener {

		public void onComplete(final String response, final Object state) {
			Log.d("Stock SNS-Main", "Response: " + response.toString());
			try {
				stockItems.clear();
				Log.d("Stock SNS-Main", "Response: " + response.toString());
				JSONObject json = Util.parseJson(response);
				final ArrayList<Stock> stocks = ObjUtils.jsonToStockList(json.getJSONArray("stockList"));
				
				for (Stock stock : stocks) {
					stockItems.add(stock);
				}
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						stockAdapter.notifyDataSetChanged();
						if(progressdialog.isShowing())
							progressdialog.dismiss();
					}
				});
			} catch (JSONException e) {
				Log.w("Stock SNS-Main", "JSON Error in response");
			} catch (JustplaceError e) {
				Log.w("Stock SNS-Main", "Stock SNS Error: " + e.getMessage());
			}
		}
	}

}
